#include <stdio.h>
#include <malloc.h>


struct item{
	int a;	//a*x^b
	int b;
};

typedef struct item Element;

struct node{
	Element elm;
	struct node *next;
};

typedef struct node Node;

typedef struct node* List;

List initList(void);

void destroyList(List);
//	创建多项式
//	给出的系数是降序的,不用考虑合并相同系数
void buildMulti(List,Element);
//
//	多项式相加
List addMulti(List,List);
//	合并相同系数
Element mergeItem(Element,Element);

List mulMulti(List,List);
//	多项式插入项数
void insertElement(List,Element);
//	打印多项式
void prtMulti(List);
void freeList(List);

//---------------------------------------------
//---------------------------------------------

List initList(void){
	List ret=malloc(sizeof(struct node));
	ret->next=NULL;
	return ret;
}

void destroyList(List head){
	freeList(head);
	free(head);
}
//	head 有调用函数初始化
void buildMulti(List head,Element v){
	while(head->next){
		head=head->next;
	}
	if( v.a ){
		List tmp=malloc(sizeof(struct node));
		tmp->elm=v;
		tmp->next=NULL;
		head->next=tmp;
	}
	return ;
}

//	合并相同系数
Element mergeItem(Element e1,Element e2){
	Element ret;
	ret.a=e1.a+e2.a;
	ret.b=e1.b;
	return ret;
}

//	多项式相加
List addMulti(List l1,List l2){
	List head,ret,a;
	head=ret=initList();
	l1=l1->next;
	l2=l2->next;
	while( l1 && l2 ){
		a=malloc(sizeof(struct node));
		if( (l1->elm).b == (l2->elm).b ){	//合并同系数
			Element m=mergeItem( l1->elm , l2->elm );
			l1=l1->next;
			l2=l2->next;
			if( m.a ){
				a->elm= m;
			}else{
				free(a);
				continue;
			}
		}else if ( (l1->elm).b > (l2->elm).b ){
				a->elm= l1->elm;
				l1=l1->next;
		}else{	//(l1->elm).b < (l2->elm).b)
				a->elm= l2->elm;
				l2=l2->next;
		}
		a->next=NULL;
		ret->next=a;
		ret=ret->next;
	}
	while( l1 ){
		a=malloc(sizeof(Node));
		a->elm=l1->elm;
		a->next=NULL;
		ret->next=a;
		ret=ret->next;
		l1=l1->next;
	}
	while( l2 ){
		a=malloc(sizeof(Node));
		a->elm=l2->elm;
		a->next=NULL;
		ret->next=a;
		ret=ret->next;
		l2=l2->next;
	}
	return head;
}

void prtMulti(List l){
	if( NULL == l->next ){
		printf("0 0");
		return ;
	}
	// first item
	printf("%d %d",(l->next->elm).a,(l->next->elm).b);
	l=l->next;
	while( l->next ){
		printf(" %d %d",(l->next->elm).a,(l->next->elm).b);
		l=l->next;
	}
	return ;
}


List mulMulti(List list1,List list2){
	//	返回的结果链表
	List head=initList();
	List x1,new_list;
	for( List l1=list1->next; l1 ; l1=l1->next ){
		new_list=initList();
		for( List l2=list2->next; l2; l2=l2->next ){
			Element new_elm;
			new_elm.a= (l1->elm).a * (l2->elm).a;
			new_elm.b= (l1->elm).b + (l2->elm).b;
			if( new_elm.a ){
				buildMulti(new_list,new_elm);
			}
		}
		x1=addMulti(head,new_list);
		destroyList(new_list);
		destroyList(head);
		head=x1;
	}
	return head;
}

void freeList(List list){
	List current=list->next;
	List tmp;
	while( current ){
		tmp=current->next;
		free(current);
		current=tmp;
	}
	return;
}

int main(void){
	List list[2];
	List add_ret;
	List mul_ret;
	int nums;
	Element tmp;
	for(int t=0; t<2 ;t++){
		list[t]=initList();
		scanf("%d",&nums);
		for(int i=0;i<nums;i++){
			scanf("%d %d",&(tmp.a),&(tmp.b));
			buildMulti( list[t] , tmp );
		}
	}
	mul_ret=mulMulti(list[0],list[1]);
	prtMulti( mul_ret );
printf("\n");
	add_ret=addMulti( list[0],list[1] );
	prtMulti( add_ret );
	destroyList(list[0]);
	destroyList(list[1]);
	destroyList(add_ret);
	destroyList(mul_ret);
	return 0;
}



